package com.qf.dao.impl;

import com.qf.dao.AddressDao;
import com.qf.domain.Address;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressDaoimpl implements AddressDao {
    QueryRunner qr=new QueryRunner();
    @Override
    public List<Address> selectUserAddress(Integer id) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="select * from tb_address where uid=?";
        try {
           return qr.query(connection,sql,new BeanListHandler<>(Address.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }
    @Override
    public void insertAddribute(Address address) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="insert into tb_address(id,detail,name,phone,uid,level) values(?,?,?,?,?,?);";
        Object[] params={address.getId(),address.getDetail(),address.getName(),address.getPhone(),address.getUid(),address.getLevel()};
        try {
             qr.update(connection,sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void deleteAddressUserone(Integer id) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="delete from tb_address where id=?";
        try {
            qr.update(connection,sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void updateUserAddressone(Address address) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="update tb_address set detail=?,name=?,phone=?,level=? where id=?";
        Object[] params={address.getDetail(),address.getName(),address.getPhone(),address.getLevel(),address.getId()};
        try {
            qr.update(connection,sql,params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void updatelevel(Integer id,Integer uid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="update tb_address set level=0 where uid=?";
        String sql1="update tb_address set level=1 where id=?";
        try {
            qr.update(connection,sql,uid);
            qr.update(connection,sql1,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public Object selectUserAddresslevel(Integer id) {
        Connection connection=DataSourceUtils.getConnection();
        String sql="select level from tb_address where id=?";
        try {
           return qr.query(connection,sql,new ScalarHandler(),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }

    }

    @Override
    public Address selectUserAddressOne(Integer aid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql = "select * from tb_address where id=?";
        try {
            return qr.query(connection,sql, new BeanHandler<>(Address.class), aid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtils.closeAll(null, null, connection);
        }
    }
}
